package com.ruoyi.sbgl.sbdb.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sbgl.sbdb.mapper.SbdbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sbgl.sbdb.domain.SbdbDbmx;
import com.ruoyi.sbgl.sbdb.domain.Sbdb;
import com.ruoyi.sbgl.sbdb.service.ISbdbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 设备调拨Service业务层处理
 *
 * @author huacai
 * @date 2024-09-13
 */
@Service
public class SbdbServiceImpl implements ISbdbService
{
    @Autowired
    private SbdbMapper sbdbMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询设备调拨
     *
     * @param sbdbId 设备调拨主键
     * @return 设备调拨
     */
    @Override
    public Sbdb selectSbdbBySbdbId(Integer sbdbId)
    {
        return sbdbMapper.selectSbdbBySbdbId(sbdbId);
    }

    /**
     * 查询设备调拨列表
     *
     * @param sbdb 设备调拨
     * @return 设备调拨
     */
    @Override
    public List<Sbdb> selectSbdbList(Sbdb sbdb)
    {
        return sbdbMapper.selectSbdbList(sbdb);
    }

    /**
     * 新增设备调拨
     *
     * @param sbdb 设备调拨
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSbdb(Sbdb sbdb)
    {
        sbdb.setCreateTime(DateUtils.getNowDate());
        int rows = sbdbMapper.insertSbdb(sbdb);
        insertSbdbDbmx(sbdb);
        return rows;
    }

    /**
     * 批量新增设备调拨
     *
     * @param sbdbs 设备调拨List
     * @return 结果
     */
    @Override
    public int batchInsertSbdb(List<Sbdb> sbdbs)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(sbdbs)) {
            try {
                for (int i = 0; i < sbdbs.size(); i++) {
                    int row = sbdbMapper.insertSbdb(sbdbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == sbdbs.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改设备调拨
     *
     * @param sbdb 设备调拨
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSbdb(Sbdb sbdb)
    {
        sbdb.setUpdateTime(DateUtils.getNowDate());
        sbdbMapper.deleteSbdbDbmxBySbdbId(sbdb.getSbdbId());
        insertSbdbDbmx(sbdb);
        return sbdbMapper.updateSbdb(sbdb);
    }

    /**
     * 批量删除设备调拨
     *
     * @param sbdbIds 需要删除的设备调拨主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSbdbBySbdbIds(Integer[] sbdbIds)
    {
        sbdbMapper.deleteSbdbDbmxBySbdbIds(sbdbIds);
        return sbdbMapper.deleteSbdbBySbdbIds(sbdbIds);
    }

    /**
     * 删除设备调拨信息
     *
     * @param sbdbId 设备调拨主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSbdbBySbdbId(Integer sbdbId)
    {
        sbdbMapper.deleteSbdbDbmxBySbdbId(sbdbId);
        return sbdbMapper.deleteSbdbBySbdbId(sbdbId);
    }

    /**
     * 新增设备调拨明细信息
     *
     * @param sbdb 设备调拨对象
     */
    public void insertSbdbDbmx(Sbdb sbdb)
    {
        List<SbdbDbmx> sbdbDbmxList = sbdb.getSbdbDbmxList();
        Integer sbdbId = sbdb.getSbdbId();
        if (StringUtils.isNotNull(sbdbDbmxList))
        {
            List<SbdbDbmx> list = new ArrayList<SbdbDbmx>();
            for (SbdbDbmx sbdbDbmx : sbdbDbmxList)
            {
                sbdbDbmx.setSbdbId(sbdbId);
                list.add(sbdbDbmx);
            }
            if (list.size() > 0)
            {
                sbdbMapper.batchSbdbDbmx(list);
            }
        }
    }
}
